<?php
	$lp_root_path='../';

	$general_root_path = '../../';

	define('INSIDE', true);
	include($general_root_path . 'extension.inc');
	include($general_root_path . 'common.'.$phpEx);
include_once($lp_root_path. 'classes/templates.class.'.$phpEx);
include_once($lp_root_path. 'procedures.class.'.$phpEx);
include_once($lp_root_path. 'classes/json_data_classes.'.$phpEx);

$dentro=check_user();


$user_id = $_SESSION['id_usuario'];

$result = new json_request();

/*
ERROR LIST:

-1		->	No session
-2		->	No request data, missing "jsondata" parameter
-3		->	Type not implemented
-4		->	Operation not implemented
-5		->	Exception

*/

if(!$dentro)
{
	$result->error=1;
	$result->error_number=-1;
	echo "no_session";
}
else
{
	/*if($jsonraw==null)
	{
		$result->error=1;
		$result->error_number=-2;
		echo "bad_request";
	}
	else
	{*/
		/*$jsonraw = str_replace('\"','"',$jsonraw);
		$request = new json_request();
		$request->load(json_decode($jsonraw));*/

		
	$sp = new procedures();

	try
	{
		$row = $sp->sp_users_getMessageList($user_id);
						
		$messageRow = "";
		$messageSection = '<table id="[SECTION]" border="1" width="95%">
								<caption>[SECTION]</caption>
								<tr>
									<th>ID</th>
									<th>TIME</th>
									<th>FROM USER</th>
									<th>SUMARY</th>
									<th>READ</th>
								</tr>
								[ROWS]
							</table><hr />';
							
		$building = "";
		$research = "";
		$hangar = "";
		$fleet = "";
		$admin = "";
		$message = "";
		$report = "";
		
		//Comento este bucle que no se que hace. 
		//while ($row =array_pop( $dataRow))
		
		{
			$messageRow = "<tr onclick='showMessage(".$dataRow["id"].");'><td>".$dataRow["id"]."</td><td>".$dataRow["time"]."</td><td>".$dataRow["fromUserId"]."</td><td>".$dataRow["sumary"]."</td><td>".$dataRow["isRead"]."</td></tr>";
			switch($dataRow["type"])
			{
				case "admin":
					$admin = $admin.$messageRow;
					break;
				case "building":
					$building = $building.$messageRow;
					break;
				case "research":
					$research = $research.$messageRow;
					break;
				case "hangar":
					$hangar = $hangar.$messageRow;
					break;
				case "fleet":
					$fleet = $fleet.$messageRow;
					break;
				case "report":
					$report = $report.$messageRow;
					break;
				case "message":
					$message = $message.$messageRow;
					break;
				default:
					break;
			}
		}
	}
	catch (exception $e)
	{
		die($e->getMessage());
	}
	/*}*/
}

$completeSection = str_replace("[ROWS]",$admin,str_replace("[SECTION]","admin",$messageSection)) .
	str_replace("[ROWS]",$building,str_replace("[SECTION]","building",$messageSection)) .
	str_replace("[ROWS]",$research,str_replace("[SECTION]","research",$messageSection)) .
	str_replace("[ROWS]",$hangar,str_replace("[SECTION]","hangar",$messageSection)) .
	str_replace("[ROWS]",$fleet,str_replace("[SECTION]","fleet",$messageSection)) .
	str_replace("[ROWS]",$message,str_replace("[SECTION]","message",$messageSection)) .
	str_replace("[ROWS]",$report,str_replace("[SECTION]","report",$messageSection));

echo $completeSection;
?>
